﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using MpSoft.Library.DTO;
using MpSoft.Basic.DataAccess;

namespace MpSoft.Library.DAO.Contents
{
   internal class NewsDAO
    {
        #region general

        private static News LoadObj(IDataReader datareader)
        {
            News obj = new News();
            obj.NewsID = MpDataHelp.GetInt(datareader, "NewsID");
            obj.CategoryID = MpDataHelp.GetInt(datareader, "CategoryID");
            obj.Count = MpDataHelp.GetInt(datareader, "Count");
            obj.Title = MpDataHelp.GetString(datareader, "Title");
            obj.Description = MpDataHelp.GetString(datareader, "Description");
            obj.Image = MpDataHelp.GetString(datareader, "Image");
            obj.Content = MpDataHelp.GetString(datareader, "Content");
            obj.MetaTitle = MpDataHelp.GetString(datareader, "MetaTitle");
            obj.MetaKeyword = MpDataHelp.GetString(datareader, "MetaKeyword");
            obj.MetaDescription = MpDataHelp.GetString(datareader, "MetaDescription");
            obj.SeName = MpDataHelp.GetString(datareader, "SeName");
            obj.FormatUrl = MpDataHelp.GetString(datareader, "FormatUrl");
            obj.IsActive = MpDataHelp.GetBoolean(datareader, "IsActive");
            obj.IsHome = MpDataHelp.GetBoolean(datareader, "IsHome");
            obj.IsTypical = MpDataHelp.GetBoolean(datareader, "IsTypical");
            obj.SortOrder = MpDataHelp.GetInt(datareader, "SortOrder");
            obj.DateCreate = MpDataHelp.GetDateTime(datareader, "DateCreate");

            return obj;
        }

        #endregion

        public static int News_Add(News obj)
        {
            Database db = MpDataHelp.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Mp_News_Add");
            db.AddOutParameter(dbCommand, "NewsID", DbType.Int32, obj.NewsID);
            db.AddInParameter(dbCommand, "CategoryID", DbType.Int32, obj.CategoryID);
            db.AddInParameter(dbCommand, "Count", DbType.Int32, obj.Count);
            db.AddInParameter(dbCommand, "SortOrder", DbType.Int32, obj.SortOrder);
            db.AddInParameter(dbCommand, "Title", DbType.String, obj.Title);
            db.AddInParameter(dbCommand, "Description", DbType.String, obj.Description);
            db.AddInParameter(dbCommand, "Image", DbType.String, obj.Image);
            db.AddInParameter(dbCommand, "Content", DbType.String, obj.Content);
            db.AddInParameter(dbCommand, "MetaTitle", DbType.String, obj.MetaTitle);
            db.AddInParameter(dbCommand, "MetaKeyword", DbType.String, obj.MetaKeyword);
            db.AddInParameter(dbCommand, "MetaDescription", DbType.String, obj.MetaDescription);
            db.AddInParameter(dbCommand, "SeName", DbType.String, obj.SeName);
            db.AddInParameter(dbCommand, "FormatUrl", DbType.String, obj.FormatUrl);
            db.AddInParameter(dbCommand, "IsActive", DbType.Boolean, obj.IsActive);
            db.AddInParameter(dbCommand, "IsHome", DbType.Boolean, obj.IsHome);
            db.AddInParameter(dbCommand, "IsTypical", DbType.Boolean, obj.IsTypical);

            if (db.ExecuteNonQuery(dbCommand) > 0)
                return Convert.ToInt32(db.GetParameterValue(dbCommand, "@NewsID"));
            return -1;
        }

        public static int News_Update(News obj)
        {
            Database db = MpDataHelp.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Mp_News_Update");
            db.AddInParameter(dbCommand, "NewsID", DbType.Int32, obj.NewsID);
            db.AddInParameter(dbCommand, "CategoryID", DbType.Int32, obj.CategoryID);
            db.AddInParameter(dbCommand, "Count", DbType.Int32, obj.Count);
            db.AddInParameter(dbCommand, "SortOrder", DbType.Int32, obj.SortOrder);
            db.AddInParameter(dbCommand, "Title", DbType.String, obj.Title);
            db.AddInParameter(dbCommand, "Description", DbType.String, obj.Description);
            db.AddInParameter(dbCommand, "Image", DbType.String, obj.Image);
            db.AddInParameter(dbCommand, "Content", DbType.String, obj.Content);
            db.AddInParameter(dbCommand, "MetaTitle", DbType.String, obj.MetaTitle);
            db.AddInParameter(dbCommand, "MetaKeyword", DbType.String, obj.MetaKeyword);
            db.AddInParameter(dbCommand, "MetaDescription", DbType.String, obj.MetaDescription);
            db.AddInParameter(dbCommand, "SeName", DbType.String, obj.SeName);
            db.AddInParameter(dbCommand, "FormatUrl", DbType.String, obj.FormatUrl);
            db.AddInParameter(dbCommand, "IsActive", DbType.Boolean, obj.IsActive);
            db.AddInParameter(dbCommand, "IsHome", DbType.Boolean, obj.IsHome);
            db.AddInParameter(dbCommand, "IsTypical", DbType.Boolean, obj.IsTypical);

            if (db.ExecuteNonQuery(dbCommand) > 0)
                return 1;
            return -1;
        }

        public static int News_Delete(int NewsID)
        {
            Database db = MpDataHelp.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Mp_News_Delete");
            db.AddInParameter(dbCommand, "NewsID", DbType.Int32, NewsID);

            if (db.ExecuteNonQuery(dbCommand) > 0)
                return 1;
            return -1;
        }

        public static List<News> News_List(int NewsID, int CategoryID, int IsActive, int SortOrder,
            string Title, int NumberRow, bool IsHome, bool IsTypical)
        {
            Database db = MpDataHelp.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Mp_News_List");
            db.AddInParameter(dbCommand, "NewsID", DbType.Int32, NewsID);
            db.AddInParameter(dbCommand, "CategoryID", DbType.Int32, CategoryID);
            db.AddInParameter(dbCommand, "IsActive", DbType.String, IsActive);
            db.AddInParameter(dbCommand, "SortOrder", DbType.String, SortOrder);
            db.AddInParameter(dbCommand, "Title", DbType.String, Title);
            db.AddInParameter(dbCommand, "NumberRow", DbType.String, NumberRow);
            db.AddInParameter(dbCommand, "IsHome", DbType.String, IsHome);
            db.AddInParameter(dbCommand, "IsTypical", DbType.String, IsTypical);
            List<News> listObj = null;
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    (listObj = new List<News>()).Add(LoadObj(dataReader));
                    while (dataReader.Read())
                        listObj.Add(LoadObj(dataReader));
                }
            }
            return listObj;
        }

        public static List<News> News_AllPaged(int CategoryID, int IsActive, int SortOrder,
            string Keywords, int PageIndex, int PageSize, out int TotalRow)
        {
            Database db = MpDataHelp.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Mp_News_AllPaged");
            db.AddInParameter(dbCommand, "CategoryID", DbType.Int32, CategoryID);
            db.AddInParameter(dbCommand, "IsActive", DbType.Int32, IsActive);
            db.AddInParameter(dbCommand, "SortOrder", DbType.Int32, SortOrder);
            db.AddInParameter(dbCommand, "Keywords", DbType.String, Keywords);
            db.AddInParameter(dbCommand, "PageIndex", DbType.Int32, PageIndex);
            db.AddInParameter(dbCommand, "PageSize", DbType.Int32, PageSize);
            List<News> listObj = null;
            TotalRow = 0;
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    (listObj = new List<News>()).Add(LoadObj(dataReader));
                    TotalRow = MpDataHelp.GetInt(dataReader, "TotalRow");
                    while (dataReader.Read())
                        listObj.Add(LoadObj(dataReader));
                }
            }
            return listObj;
        }
    }
}
